package com.chuji.crmcj.act;

import android.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.DisposableAdapter;
import com.chuji.crmcj.bean.DisposableDelInfo;
import com.chuji.crmcj.bean.DisposableInfo;
import com.chuji.crmcj.bean.SureLoseReadInfo;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

/**
 * Created by w.pitt on 2016/1/20.
 * 投诉
 */
public class DisposableAct extends BaseActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {

    private ListView mLvDisposable;
    private AlertDialog deleteDialog;
    private DisposableAdapter adapter;
    private DisposableInfo disposableInfo;
    private String userID;
    /*确认已读*/
    private SureLoseReadInfo sureLoseReadInfo;
    @Override
    protected void initView() {
        setContentView(R.layout.act_disposable);
        mLvDisposable = (ListView) findViewById(R.id.lv_disposable);
    }

    @Override
    protected void initData() {
        userID = SPUtils.getString(UIUtils.getContext(), "UserID", "");
        mTvBarTitle.setText("投诉");


        reqDatas();
    }

    @Override
    protected void initListener() {
        mBtnBarLeftArr.setOnClickListener(this);
        mLvDisposable.setOnItemLongClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_left_arr:
                finish();
                break;
        }
    }

    private int currentPositio = -1;

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
        AlertDialog.Builder builder = new AlertDialog.Builder(DisposableAct.this);
        deleteDialog = builder.create();
        View dialogView = View.inflate(this, R.layout.layout_delete_item, null);// 给dialog设定特定布局
        deleteDialog.setView(dialogView, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本

        Button btn_sure = (Button) dialogView.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) dialogView.findViewById(R.id.btn_cancel);

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reqDelDialog(disposableInfo.getApiParamObj().get(position).getTaskID());
                currentPositio = position;
                deleteDialog.dismiss();
            }
        });

        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteDialog.dismiss();
            }
        });
        deleteDialog.show();

        return true;
    }

    private void reqDelDialog(String taskID) {
        String strURL = String.format(UrlUtils.ComplaintDelPeter +
                "&UserID=%s&TaskID=%s", userID, taskID);
        volleyRequest(strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                final DisposableDelInfo disposableDelInfo = JSON.parseObject(s, DisposableDelInfo.class);
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        disposableInfo.getApiParamObj().remove(currentPositio);
                        adapter.notifyDataSetChanged();
                        if (disposableDelInfo.getApiParamObj().get(0).getIsok() == 1) {
                            UIUtils.showToastSafe("投诉删除成功");
                        }
                    }
                });
            }
        });
    }

    private void reqDatas() {
        String strURL = String.format(UrlUtils.ComplainPeter +
                "&UserID=%s", userID);
        volleyRequest(strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                disposableInfo = JSON.parseObject(s, DisposableInfo.class);
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new DisposableAdapter(DisposableAct.this, disposableInfo);
                        mLvDisposable.setAdapter(adapter);
                    }
                });
            }
        });
    }
    //获取读取信息
    public void SureRead(String UserID,String Remark,String BusinessId){
        String strURL = String.format(UrlUtils.SUBMIT_Lose_ISREAD+"&UserID="+UserID+"&Remark="+Remark+"&BusinessId="+BusinessId);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                sureLoseReadInfo = JSON.parseObject(response,SureLoseReadInfo.class);
                if(sureLoseReadInfo.getApiParamObj().get(0).getIsok().equals("0")){
                    UIUtils.showToastSafe("消息已读");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }
}
